package com.uoko.plugins.jpush.common;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.CIDResult;
import cn.jpush.api.push.GroupPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.uoko.plugins.jpush.config.JpushProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 极光推送推成集成
 *
 * @author shaohua
 * Email shaohua@uoko.com
 * created by 2018/5/30
 */
@Component
public class JpushSenderServer implements JpushSender {

    @Autowired
    private JpushProperties jpushProperties;

    @Override
    public CIDResult getCidList(int count, String type) throws APIConnectionException, APIRequestException {
        JPushClient jPushClient = new JPushClient(jpushProperties.getMasterSecret(), jpushProperties.getAppKey());
        return jPushClient.getCidList(count, type);
    }

    @Override
    public PushResult sendAllBroadcast(String alert, String title, String cid,
                                       Map<String, String> extras) throws Exception {
        JPushClient jpushClient = new JPushClient(jpushProperties.getMasterSecret(), jpushProperties.getAppKey());
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .setAlert(alert)
                        .addPlatformNotification(
                                AndroidNotification.newBuilder()
                                        .addExtras(extras)
                                        .setTitle(title)
                                        .setAlert(alert)
                                        .build()
                        )
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .addExtras(extras)
                                .build())
                        .build()
                )
                .setCid(cid)
                .build();
        return jpushClient.sendPush(payload);
    }

    @Override
    public PushResult sendAndroidBroadcast(String alert, String title,
                                           String cid, Map<String, String> extras) throws Exception {
        JPushClient jpushClient = new JPushClient(jpushProperties.getMasterSecret(),
                jpushProperties.getAppKey());
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(alert, title, extras))
                .setCid(cid)
                .build();
        return jpushClient.sendPush(payload);
    }

    @Override
    public PushResult sendAndroidUnicast(String alert, String title, String cid, Map<String, String> extras,
                                         String... registrationId) throws Exception {
        JPushClient jpushClient = new JPushClient(jpushProperties.getMasterSecret(),
                jpushProperties.getAppKey());
        return jpushClient.sendAndroidNotificationWithRegistrationID(title, alert, extras, registrationId);
    }

    @Override
    public Map<String, PushResult> sendAndroidGroupcast(String alert, String title,
                                                        String cid, Map<String, String> extras) throws Exception {
        GroupPushClient groupPushClient = new GroupPushClient(jpushProperties.getGroupMasterSecret(),
                jpushProperties.getGroupPushKey());
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .setAlert(alert)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .addExtras(extras).build())
                        .build())
                .setCid(cid)
                .build();
        return groupPushClient.sendGroupPush(payload);
    }

    @Override
    public PushResult sendAndroidCustomizedcast(String alert, String title,
                                                String cid, Map<String, String> extras, String... alias) throws Exception {
        JPushClient jPushClient = new JPushClient(jpushProperties.getMasterSecret(), jpushProperties.getAppKey());
        return jPushClient.sendAndroidNotificationWithAlias(title, alert, extras, alias);
    }


    @Override
    public PushResult sendIOSBroadcast(String alert, String cid, Map<String, String> extras) throws Exception {
        JPushClient jPushClient = new JPushClient(jpushProperties.getMasterSecret(), jpushProperties.getAppKey());
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(
                        Notification.newBuilder()
                                .setAlert(alert)
                                .addPlatformNotification(IosNotification.newBuilder()
                                        .addExtras(extras)
                                        .build())
                                .build())
                .setCid(cid)
                .build();
        return jPushClient.sendPush(payload);
    }

    @Override
    public PushResult sendIOSUnicast(String alert, Map<String, String> extras, String... registrationId) throws Exception {
        JPushClient jPushClient = new JPushClient(jpushProperties.getMasterSecret(), jpushProperties.getAppKey());
        return jPushClient.sendIosNotificationWithRegistrationID(alert, extras, registrationId);
    }

    @Override
    public Map<String, PushResult> sendIOSGroupcast(String alert, String cid, Map<String, String> extras) throws Exception {
        GroupPushClient groupPushClient = new GroupPushClient(jpushProperties.getGroupMasterSecret(),
                jpushProperties.getGroupPushKey());
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .setAlert(alert)
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtras(extras).build())
                        .build())
                .setCid(cid)
                .build();
        return groupPushClient.sendGroupPush(payload);
    }

    @Override
    public PushResult sendIOSCustomizedcast(String alert, String cid, Map<String, String> extras, String... alias) throws Exception {
        JPushClient jPushClient = new JPushClient(jpushProperties.getMasterSecret(), jpushProperties.getAppKey());
        return jPushClient.sendIosNotificationWithAlias(alert, extras, alias);
    }


}
